package com.qdxwx.manage.controller.comment;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.article.ArticleService;
import com.qdxwx.data.service.comment.CommentService;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.article.Article;
import com.qdxwx.models.comment.Comment;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程评论Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseNodeService courseNodeService;

    @Permissions("company:list")
    @GetMapping("/list.html")
    public String list() {
        return "comment/comment-list";
    }

    @GetMapping("/info/list.html")
    public String comment(HttpServletRequest request, ModelMap mmpa) {
        int otherId = TKConvert.toInteger(request.getParameter("otherId"));
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (otherId < 1 || typeId < 1) return error404;
        mmpa.put("otherId", otherId);
        mmpa.put("typeId", typeId);
        return "comment/comment-info-list";
    }

    /**
     * 查询课程评论列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int otherId = TKConvert.toInteger(request.getParameter("otherId"));
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        Comment comment = new Comment();
        comment.setOtherId(otherId);
        comment.setTypeId(typeId);
        comment.setStatus(TKConvert.toInteger(request.getParameter("status"),null));
        List<Comment> list = commentService.getList(comment, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(commentService.count(comment));
        return rspData;
    }

    //评论详情
    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmpa) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        Comment comment = commentService.get(id);
        if(comment == null) return error404;
        String objName = "未知";
        switch (comment.getTypeId()){
            case 1:
                Article article = articleService.get(comment.getOtherId());
                if(article != null) objName = article.getTitle();
                break;
            case 2:
                Course course = courseService.get(comment.getOtherId());
                if(course != null) objName = course.getCourseName();
                break;
            case 3:
                Course courseLive = courseService.get(comment.getOtherId());
                if(courseLive != null) objName = courseLive.getCourseName();
                break;
            case 4:
                CourseNode courseNode = courseNodeService.get(comment.getOtherId());
                if(courseNode != null) objName = courseNode.getName();
                break;
        }
        mmpa.put("objName",objName);
        mmpa.put("comment",comment);
        return "comment/comment-view";
    }

    /**
     * 审核评论
     */
    @Log(title = "评论审核", businessType = BusinessType.UPDATE)
    @PostMapping("/status.json")
    @ResponseBody
    public AjaxResult status(HttpServletRequest request) {
        int commentId = TKConvert.toInteger(request.getParameter("commentId"));
        Integer status = TKConvert.toInteger(request.getParameter("status"),null);
        if (commentId < 1) return error("参数错误，commentId="+commentId);
        if (status == null) return error("参数错误，status=null");
        Comment comment = commentService.get(commentId);
        if(comment == null) return error("找不到指定数据，commentId="+commentId);
        comment.setStatus(status);
        return toAjax(commentService.updateStatus(comment));
    }

    /**
     * 删除课程评论
     */
    @Log(title = "课程评论", businessType = BusinessType.DELETE)
    @Permissions("comment:remove")
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("参数缺少id");
        return toAjax(commentService.delete(id));
    }
}
